//
//  YHAESHelper.m
//  SunsetRed
//
//  Created by MacBook on 2023/4/3.
//  Copyright © 2023 lyh. All rights reserved.
//

#import "YHAESHelper.h"
#import <CommonCrypto/CommonCryptor.h>


@implementation YHAESHelper

static NSString *publicIv = @"209830ce7f5df31b";

+ (NSString *)getRandomNumberString {
    static int kNumber = 16;
    NSString *sourceStr = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++) {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

+ (NSString *)aesEncryptString:(NSString *)content publicKey:(NSString *)publicKey {
    return [self aesEncryptString:content withKey:publicKey withIv:publicIv];
}

+ (NSString *)aesDecryptString:(NSString *)content publicKey:(NSString *)publicKey {
    return [self aesDecryptString:content withKey:publicKey withIv:publicIv];
}

+ (NSString *)aesEncryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString *)iv {
    NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encrptData = [self aesEncryptData:contentData withKey:keyData withIv:ivData];
    return [encrptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

+ (NSString *)aesDecryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString *)iv {
    NSData *contentData = [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *decryptedData = [self aesDecryptData:contentData withKey:keyData withIv:ivData];
    return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
}

+ (NSData *)aesEncryptData:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData *)ivData {
    return [self cipherAction:contentData withKey:keyData withIv:ivData withOperation:kCCEncrypt];
}

+ (NSData *)aesDecryptData:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData *)ivData {
    return [self cipherAction:contentData withKey:keyData withIv:ivData withOperation:kCCDecrypt];
}


+ (NSData *)cipherAction:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData *)ivData withOperation:(CCOperation)operation {
    NSUInteger dataLength = contentData.length;

    void const *ivBytes = ivData.bytes;
    void const *contentBytes = contentData.bytes;
    void const *keyBytes = keyData.bytes;

    size_t bufferSize = dataLength + kCCKeySizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyBytes,
                                          kCCKeySizeAES128,
                                          ivBytes,
                                          contentBytes,
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);

    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    return nil;
}

@end
